/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.etop.ondoc.beans;

import java.util.HashSet;
import java.util.Objects;
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import org.hibernate.annotations.Cascade;
import org.hibernate.annotations.Fetch;
import org.hibernate.annotations.FetchMode;

/**
 * 设置了表示记录是否有效的常量
 * @author jansing lz_jansing@163.com
 */
@Entity
@Table(name = "t_category")
public class Category {
    public static final int IS_VAILD = 1;
    public static final int IS_NOT_VAILD = 0;
    
    private int id;
    private String name;
    /**
     * 该分类下的文档数量
     */
    private int docuCount;
    /**
     * 1：有效，0：无效
     */
    private int isVaild;
    /**
     * 子分类
     */
    private Set<Category> cateSet;
    /**
     * 该分类下的文档
     */
    private Set<Document> docuSet;
    /**
     * 父分类
     */
    private Category pareCategory;

    /**
     * isVaild、cateSet、docuSet属性已初始化，<br/>
     * 其他属性都要初始化<br/>
     */
    public Category() {
        this.isVaild = IS_VAILD;
        cateSet = new HashSet<>();
        docuSet = new HashSet<>();
    }

    @Id
    @GeneratedValue
    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    @Column(name = "name")
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    /**
     * 该分类下的文档数量
     */
    @Column(name = "d_count")
    public int getDocuCount() {
        return docuCount;
    }

    /**
     * 该分类下的文档数量
     */
    public void setDocuCount(int docuCount) {
        this.docuCount = docuCount;
    }

    /**
     * 1：有效，0：无效
     *
     * @return
     */
    @Column(name = "is_vaild")
    public int getIsVaild() {
        return isVaild;
    }

    /**
     * 1：有效，0：无效
     *
     * @param isVaild
     */
    public void setIsVaild(int isVaild) {
        this.isVaild = isVaild;
    }

    /**
     * 子分类<br/>
     * 因为分类是一次性全部获取，所以不懒加载
     */
    @OneToMany(mappedBy = "pareCategory", cascade = {CascadeType.REMOVE}, fetch=FetchType.EAGER)
    @Fetch(FetchMode.JOIN)
    public Set<Category> getCateSet() {
        return cateSet;
    }

    /**
     * 子分类
     */
    public void setCateSet(Set<Category> cateSet) {
        this.cateSet = cateSet;
    }

    /**
     * 该分类下的文档
     */
    @OneToMany(mappedBy = "category", fetch = FetchType.LAZY, cascade = {CascadeType.REMOVE})
    public Set<Document> getDocuSet() {
        return docuSet;
    }

    /**
     * 该分类下的文档
     */
    public void setDocuSet(Set<Document> docuSet) {
        this.docuSet = docuSet;
    }

    /**
     * 父分类<br/>
     * 只有在浏览文档的时候显示，所以懒加载
     */
    @ManyToOne(fetch = FetchType.LAZY,targetEntity = Category.class)
    @JoinColumn(name = "c_id")
    @Cascade(org.hibernate.annotations.CascadeType.SAVE_UPDATE)
    public Category getPareCategory() {
        return pareCategory;
    }

    /**
     * 父分类
     */
    public void setPareCategory(Category pareCategory) {
        this.pareCategory = pareCategory;
    }

    @Override
    public int hashCode() {
        int hash = 5;
        hash = 37 * hash + Objects.hashCode(this.name);
        return hash;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final Category other = (Category) obj;
        if (!Objects.equals(this.name, other.name)) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        String s = null;
        try{
            s = "Category{" + "id=" + id + ", name=" + name + ", d_Count=" + docuCount + ", isVaild=" + isVaild + '}';
        }catch(Exception e){
            System.out.println("Category 实体类的toString 方法错误，请查看源码");
        }
        return s;
    }

}
